package com.example.hanfu.collect;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.hanfu.WebViewActivity;
import com.example.hanfu.firstpage.HanFu;
import com.example.hanfu.firstpage.HanFuAdapter;
import com.example.hanfu.login.LoginState;
import com.example.hanfu.R;

import java.util.ArrayList;
import java.util.List;

public class HisActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    HanFuAdapter adapter;
    LoginState loginState=new LoginState();
    private static final String TAG = "HisActivity";
    ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        CollectDBmanager manager=new CollectDBmanager(HisActivity.this);
        Log.i(TAG,"当前用户名："+loginState.getUserName());
        Intent intent=getIntent();

        List<Collect> item=manager.listAll(intent.getStringExtra("table"),loginState.getUserName());

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_his);
        listView=findViewById(R.id.history_list);
        ArrayList<HanFu> hanFu=new ArrayList<HanFu>();
        for(int i=0;i<item.size();i++){
            hanFu.add(item.get(i).getHanFu());
        }


        adapter = new HanFuAdapter(HisActivity.this, R.layout.list_item, hanFu);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        HanFu item =(HanFu) listView.getItemAtPosition(position);
        String title=item.getTitle();
        String href=item.getHref();
        //Bitmap img=item.getImg();
        Log.i(TAG, "onItemClick:title" + title);
        Log.i(TAG, "onItemClick:href:" + href);
        //Log.i(TAG, "onItemClick:img:" + img);
        Intent openHref=new Intent(HisActivity.this, WebViewActivity.class)
                .putExtra("url",href);
        Log.i(TAG, "openWebViewActivity:url=" + href);
        startActivity(openHref);
    }
}